package main.Q1301_1400;

import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

public class Q1381_1390 {
	public static void main(String[] args) {
		System.out.println("Question1381：设计一个支持增量操作的栈");
		System.out.println("Question1382：");
		System.out.println("Question1383：");
		System.out.println("Question1384：");
		System.out.println("Question1385：两个数组间的距离值");
		System.out.println("Question1386：");
		System.out.println("Question1387：");
		System.out.println("Question1388：");
		System.out.println("Question1389：");
		System.out.println("Question1390：");
	}
}

class Question1381 {
	Deque<Integer> queue = new LinkedList<>();
	int maxSize = -1;

	public Question1381(int maxSize) {
		this.maxSize = maxSize;
	}

	public void push(int x) {
		if (queue.size() < maxSize) {
			queue.add(x);
		}
	}

	public int pop() {
		if (queue.isEmpty()) return -1;
		return queue.pollLast();
	}

	public void increment(int k, int val) {
		Deque<Integer> temp = new LinkedList<>();
		int size = queue.size();
		for (int i = 0; i < k && i < size; i++) {
			temp.add(queue.pollFirst() + val);
		}
		while (!temp.isEmpty()) {
			queue.addFirst(temp.pollLast());
		}
	}
}

class Question1385 {
	public int findTheDistanceValue(int[] arr1, int[] arr2, int d) {
		Arrays.sort(arr1);
		Arrays.sort(arr2);
		int count = 0, length1 = arr1.length, length2 = arr2.length;
		for (int i = 0; i < length1; i++) {
			int max = arr1[i] + d, min = arr1[i] - d;
			if (max < arr2[0] || min > arr2[length2 - 1]) count++;
			else {
				int start = 0, end = length2 - 1;
				while (!(arr2[start] >= min)) start++;
				while (!(arr2[end] <= max)) end--;
				if (end - start < 0) count++;
			}
		}
		return count;
	}
}